#Region "Comments"
'  Module                       :    Business Layer's  Random number generator.
'  Purpose                      :    To get unique IDs.
#End Region

Imports System.Data.SqlClient

Namespace BusinessLayer
    Public Class RndmNoGenerator

#Region "Transaction"

        ''' <summary>
        ''' To randomize password.
        ''' </summary>
        ''' <param name="myLength"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>

        Public Function RandomPW(ByVal myLength)
            'These constant are the minimum and maximum length for random
            'length passwords.  Adjust these values to your needs.
            Const minLength = 6
            Const maxLength = 20

            Dim X, Y, strPW
            strPW = ""
            '  Try

            If myLength = 0 Then
                Randomize()
                myLength = Int((maxLength * Rnd()) + minLength)
            End If


            For X = 1 To myLength
                'Randomize the type of this character
                Y = Int((3 * Rnd()) + 1) '(1) Numeric, (2) Uppercase, (3) Lowercase

                Select Case Y
                    Case 1
                        'Numeric character
                        Randomize()
                        strPW = strPW & Chr(Int((9 * Rnd()) + 48))
                    Case 2
                        'Uppercase character
                        Randomize()
                        strPW = strPW & Chr(Int((25 * Rnd()) + 65))
                    Case 3
                        'Lowercase character
                        Randomize()
                        strPW = strPW & Chr(Int((25 * Rnd()) + 97))

                End Select
            Next

            RandomPW = Trim(strPW)
            'Catch ex As Exception
            '    Throw ex
            'End Try

        End Function

        ''' <summary>
        ''' To get unique Investor Code.
        ''' </summary>
        ''' <param name="pSQLTransaction"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>

        Public Function GetUniqueInvestorCode(Optional ByVal pSQLTransaction As SqlTransaction = Nothing) As String

            Dim strInvestorCode As String
            Dim objExecutionResult As New ExecutionResult
            Dim Exists As Integer
            'Try
            strInvestorCode = mstrInvestorPrefix
            strInvestorCode = Trim(strInvestorCode & RandomPW(8))
            If pSQLTransaction Is Nothing Then
                objExecutionResult = CoreFunctions.Execute(strConfiguration, "select count(*) from CP_Investors where Entity_ID ='" & Trim(strInvestorCode) & "'", CommandType.Text, ReturnTypes.Scalar)
            Else
                objExecutionResult = CoreFunctions.Execute(pSQLTransaction, "select count(*) from CP_Investors where Entity_ID ='" & Trim(strInvestorCode) & "'", CommandType.Text, ReturnTypes.Scalar)
            End If
            Exists = objExecutionResult.ResultObject
            If Exists = 0 Then
                Return strInvestorCode
            Else
                Return Nothing
            End If
            'Catch ex As Exception
            '    Return Nothing
            'Finally
            '    objExecutionResult = Nothing
            'End Try
        End Function

        ''' <summary>
        ''' To get unique Fund Code.
        ''' </summary>
        ''' <param name="pSQLTransaction"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>

        Public Function GetUniqueFundCode(Optional ByVal pSQLTransaction As SqlTransaction = Nothing) As String

            Dim strFundCode As String
            Dim objExecutionResult As New ExecutionResult
            Dim Exists As Integer
            'Try
            strFundCode = mstrFundPrefix
            strFundCode = Trim(strFundCode & RandomPW(8))
            If pSQLTransaction Is Nothing Then
                objExecutionResult = CoreFunctions.Execute(strConfiguration, "select count(*) from CP_Funds where Entity_ID ='" & Trim(strFundCode) & "'", CommandType.Text, ReturnTypes.Scalar)
            Else
                objExecutionResult = CoreFunctions.Execute(pSQLTransaction, "select count(*) from CP_Funds where Entity_ID ='" & Trim(strFundCode) & "'", CommandType.Text, ReturnTypes.Scalar)
            End If
            Exists = objExecutionResult.ResultObject
            If Exists = 0 Then
                Return strFundCode
            Else
                Return Nothing
            End If
            'Catch ex As Exception
            '    Return Nothing
            'Finally
            '    objExecutionResult = Nothing
            'End Try

        End Function

        ''' <summary>
        ''' To get SysHolder ID.
        ''' </summary>
        ''' <param name="strnName"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>

        Public Function GenSysHolderId(ByVal strnName As String) As String
            Dim strSysHolderId As String
            'Try

            strSysHolderId = strnName & GenRandomNumber()

            'Catch ex As Exception
            '    Throw ex
            'End Try
            Return strSysHolderId
        End Function

        ''' <summary>
        ''' To generate random numbers.
        ''' </summary>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>

        Public Function GenRandomNumber() As Integer
            Static RandomNumGen As New System.Random
            Dim intRnd As Int16 = Convert.ToInt16(RandomNumGen.Next(1000, 20000 + 1))
            Return intRnd
        End Function

#End Region

    End Class
End Namespace